package leetcode_201_300;

public class LeeCode_213 {
    public static void main(String[] args) {
        System.out.println(rob(new int[]{2, 1, 1, 2}));
        System.out.println(rob(new int[]{1,2,3,1}));
        System.out.println(rob(new int[]{2,7,9,3,1}));
    }
    private static int rob(int[] nums) {
        if (nums.length == 1)
            return nums[0];
        int n = nums.length;
        int[] dp1 = new int[n + 1];
        for (int i = 2; i < n + 1; i++) {
            dp1[i] = Math.max(dp1[i - 2] + nums[i - 2], dp1[i - 1]);
        }
        int[] dp2 = new int[n + 1];
        for (int i = 2; i < n + 1; i++) {
            dp2[i] = Math.max(dp2[i - 2] + nums[i - 1], dp2[i - 1]);
        }
        return Math.max(dp1[n], dp2[n]);
    }
}
